const EnumRoleType = {   // 用户角色类型
  ADMIN: 'admin',
  DEFAULT: 'guest',
  USER: 'user',
};

const userPermission = {  // 用户权限
  DEFAULT: {
    role: EnumRoleType.DEFAULT,
    menu: [{
      name: 'Dashboard',
      icon: 'dashboard',
      path: 'dashboard',
    }],
  },
  USER: {
    role: EnumRoleType.USER,
    menu: [{
      name: 'Dashboard',
      icon: 'dashboard',
      path: 'dashboard',
    },{
      name: '用户列表',
      icon: 'user',
      path: 'pagetest',
    }],
  },
  ADMIN: {
    role: EnumRoleType.ADMIN,
    menu: [{
      name: 'Dashboard',
      icon: 'dashboard',
      path: 'dashboard',
    },{
      name: '用户列表',
      icon: 'user',
      path: 'pagetest',
    },{
      name: '账户',
      icon: 'user',
      path: 'user',
      children: [
        {
          name: '登录',
          path: 'login',
        },
        {
          name: '注册',
          path: 'register',
        },
        {
          name: '注册结果',
          path: 'register-result',
        },
      ],
    }],
  },
};

const adminUsers = [
  {
    id: 0,
    username: 'admin',
    password: '888888',
    permissions: userPermission.ADMIN,
  }, {
    id: 1,
    username: 'guest',
    password: 'guest',
    permissions: userPermission.DEFAULT,
  }, {
    id: 2,
    username: 'user',
    password: '123456',
    permissions: userPermission.USER,
  },
];

export const getPermissions = (req, res) => {
  const { taken } = req.body;
  const user = adminUsers.filter(item => item.username === taken);
  if (user.length > 0) {
    res.status(203).json({
      success: true,
      message: '该用户已登录',
      user: user[0].username,
      permissions: user[0].permissions,
    })
  } else {
    res.status(200).json({
      success: false,
      message: '该用户还未登陆',
    })
  }
};

export const Login = (req, res) => {
  const { password, userName } = req.body;
  const user = adminUsers.filter(item => item.username === userName && item.password === password);
  if (user.length < 0){
    res.status(200).json({
      success: false,
      message: '该用户不存在，请重新注册',
    });
  }

  res.status(200).json({
    success: true,
    message: 'ok',
    user: user[0].username,
    permissions: user[0].permissions,
  });
};

export const Logout = (req, res) => {
  res.status(200).json({
    success: true,
    message: '退出成功',
    permissions: userPermission.DEFAULT,
  })
};

export const Register = (req, res) => {
  res.status(200).json({
    success: true,
    message: 'ok',
    permissions: userPermission.DEFAULT,
  })
};
